package com.qf.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;

public class DynamicDataSourceContextHolder {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    @Autowired
    static
    AbstractRoutingDataSource abstractRoutingDataSource;

    public static void setDataSourceType(String dataSourceType) {
        System.out.println("Getting data source type1: " + dataSourceType);
        contextHolder.set(dataSourceType);
    }

    public static String getDataSourceType() {
        String dataSourceType = contextHolder.get();
        System.out.println("Getting data source type2: " + dataSourceType);

        return contextHolder.get();
    }

    public static void clearDataSourceType() {

        System.out.println("Clearing data source type3");
        contextHolder.remove();
    }
}